package com.sun.local.top;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.sun.common.constants.Constant.SSQ_FILE_PATH;

/**
 * AC排行榜（按年）
 */
public class TopAc {

    public static void main(String[] args) {
        getTopAc();
    }

    public static void getTopAc() {
        // 读取文件数据
        List<String> lines = FileUtil.readUtf8Lines(SSQ_FILE_PATH);

        // 按年进行筛选
        Map<String, Map<String, String>> yearMap = filterYear(lines);

        // 按年进行排序
        Map<String, Map<String, String>> yearSortMap = sortYear(yearMap);

        // 获取输出内容
        buildOutputContent(yearSortMap);
    }

    /**
     * 按年过滤数据
     */
    private static Map<String, Map<String, String>> filterYear(List<String> lines) {
        Map<String, Map<String, String>> yearMap = new LinkedHashMap<>();
        for (String ssq : lines) {
            // 年份 例：2023-08-31
            String year = ssq.substring(0, 4);
            // AC值
            String ac = StringUtils.substringBetween(ssq, "AC=", "|").trim();
            // 次数
            Map<String, String> acMap = yearMap.get(year);
            if(acMap != null && !acMap.isEmpty()) {
                String count = acMap.get(ac);
                if(StringUtils.isBlank(count)) {
                    acMap.put(ac, "1");
                } else {
                    acMap.put(ac, String.valueOf(Integer.parseInt(count) + 1));
                }
                yearMap.put(year, acMap);
            } else {
                acMap = new HashMap<>();
                acMap.put(ac, "1");
                yearMap.put(year, acMap);
            }
        }
        return yearMap;
    }

    /**
     * 按年分类，然后按照ac出现的次数进行排序。
     */
    private static Map<String, Map<String, String>> sortYear(Map<String, Map<String, String>> yearMap) {
        Map<String, Map<String, String>> yearSortMap = new LinkedHashMap<>();
        for (Map.Entry<String, Map<String, String>> yearEntry : yearMap.entrySet()) {
            Map<String, String> acMap = yearEntry.getValue();

            // 按照字符串对应的数字类型排序排列
            Map<String, String> acSortMap = acMap.entrySet().stream()
                    .sorted((e1, e2) -> Integer.compare(Integer.parseInt(e2.getValue()), Integer.parseInt(e1.getValue())))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));

            yearSortMap.put(yearEntry.getKey(), acSortMap);
        }
        return yearSortMap;
    }

    /**
     * 构建输出内容
     */
    private static void buildOutputContent(Map<String, Map<String, String>> yearSortMap) {
        for (Map.Entry<String, Map<String, String>> yearSortEntry : yearSortMap.entrySet()) {
            String year = yearSortEntry.getKey();
            System.out.println("#########################" + year + "年数据###########################");
            for (Map.Entry<String, String> acSortEntry : yearSortEntry.getValue().entrySet()) {
                String ac = acSortEntry.getKey();
                String acCount = acSortEntry.getValue();
                System.out.println(year + "年AC值等于" + ac + "出现" + acCount + "次");
            }
        }
    }

}
